package cn.tannn.controller;

import cn.jdevelops.annotation.mapping.PathRestController;
import cn.jdevelops.result.result.ResultVO;
import cn.tannn.controller.dto.CodeDDL;
import cn.tannn.entity.ClassInfo;
import cn.tannn.service.GeneratorService;
import cn.tannn.util.StringUtils;
import cn.tannn.util.TableParseUtil;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;

/**
 * @author tn
 * @version 1
 * @date 2022-01-04 13:19
 */
@PathRestController()
public class GeneratorController {

    @Autowired
    private GeneratorService generatorService;

    /**
     * 根据ddl生成实体
     * @param codeDDL codeDDL
     * @return ResultVO
     */
    @PostMapping("autoCode")
    public ResultVO<ClassInfo> autoCodeByDDL(@RequestBody @Valid CodeDDL codeDDL) throws TemplateException, IOException {
        ClassInfo classInfo = TableParseUtil.ddlToBean(codeDDL.getSql(),codeDDL.getIgnorePrefix());
        classInfo.setAuthorName(codeDDL.getAuthorName());
        Map<String, String> resultByParams = generatorService.getResultByParams(classInfo);
        classInfo.setResultByParams(resultByParams);
        return ResultVO.success(classInfo, "查询成功");
    }

}
